package com.quartz.config;

import com.quartz.QuartzJob;
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.SimpleTrigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.*;

import java.util.Objects;

@Configuration
public class QuartzConfig {
	@Bean
	MethodInvokingJobDetailFactoryBean jobDetail1(){
		MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
		jobDetail.setTargetBeanName("quartzJob");
		jobDetail.setTargetMethod("doJob");
		return jobDetail;
	}
	@Bean
	JobDetailFactoryBean jobDetail2(){
		JobDetailFactoryBean jobDetail = new JobDetailFactoryBean();
		jobDetail.setJobClass(QuartzJob.class);
		JobDataMap map = new JobDataMap();
		map.put("orderNo","1001");
		jobDetail.setJobDataMap(map);
		jobDetail.setDurability(true);
		return jobDetail;
	}
	@Bean
	SimpleTriggerFactoryBean trigger1(){
		SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
		trigger.setJobDetail(Objects.requireNonNull(jobDetail1().getObject()));
		trigger.setRepeatCount(5);
		trigger.setStartDelay(2000);
		trigger.setRepeatInterval(5000);
		return trigger;
	}
	@Bean
	CronTriggerFactoryBean trigger2(){
		CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
		trigger.setJobDetail(Objects.requireNonNull(jobDetail2().getObject()));
		trigger.setCronExpression("0/20 * * * * ?");
		return trigger;
	}
	@Bean
	SchedulerFactoryBean schedulerFactoryBean(){
		SchedulerFactoryBean mySchedule = new SchedulerFactoryBean();
		SimpleTrigger trigger1 = trigger1().getObject();
		CronTrigger trigger2 = trigger2().getObject();
		mySchedule.setTriggers(trigger1,trigger2);
		return mySchedule;
	}
}
